################################################################################
### define species inputs
occur.file = '/home/jc148322/frog_declines/all_records.csv' #the file with your species code, lat and longs
post.file = '/home/jc148322/frog_declines/post_decline.csv'
wd = '/home/jc148322/frog_declines/'# your work directory eg."/home/jc148322/SDM/Random_post/"; 
dir.create(wd); setwd(wd) #define and set the working directory
################################################################################
library(SDMTools)
climate.dir='/home/jc165798/Climate/CIAS/AWT/250m/baseline.76to05/bioclim/'

###prepare the data
occur = read.csv(occur.file,as.is=TRUE) #read in the occur file
post = read.csv(post.file,as.is=TRUE) #read in the occur file

clim.vars = paste('bioclim_',sprintf('%02i',c(1,4,5,6,12,15,16,17)),sep='') #define the climate variables of interest
for (clim.var in clim.vars) { cat(clim.var,'\n') #cycle through each of hte climate variables
	occur[,clim.var] = extract.data(cbind(occur$longdecimal,occur$latdecimal), read.asc.gz(paste(climate.dir,clim.var,'.asc.gz',sep=''))) #append the climate data
	post[,clim.var] = extract.data(cbind(post$longdecimal,post$latdecimal), read.asc.gz(paste(climate.dir,clim.var,'.asc.gz',sep=''))) #append the climate data
		
}

occur=occur[,-grep('X',colnames(occur))]
post=post[,-grep('X',colnames(post))]

write.csv(occur,'all_records.csv',row.names=FALSE)
write.csv(post,'post_decline.csv',row.names=FALSE)
